﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;


public partial class PagerControl : UserControl
{
    public PagerControl()
    {
        Load += PagerControl_Load;
        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
        // This call is required by the Windows Windows.Forms.Form Designer.
        InitializeComponent();
        // Add any initialization after the InitializeComponent() call.
    }

    public event PageIndex_ChangedEventHandler PageIndex_Changed;
    public delegate void PageIndex_ChangedEventHandler(int pageIndex);

    private int _PageSize = 50;
    [DefaultValue(50), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public int PageSize
    {
        get { return _PageSize; }
        set
        {
            _PageSize = value;
            cboPageSize.Text = _PageSize.ToString();
        }
    }

    private bool _AllowChangePageSize = false;
    [DefaultValue(50), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public bool AllowChangePageSize
    {
        get { return _AllowChangePageSize; }
        set
        {
            _AllowChangePageSize = value;
        }
    }

    private string _CustomPageInfo = "{0}/{1} ({2})";
    [DefaultValue("{0}/{1} ({2})"), Description("Page {0: _PageIndex}/{1 : TotalPage} ({2: _TotalRow} rows)"), Browsable(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
    public String CustomPageInfo
    {
        get { return _CustomPageInfo; }
        set
        {
            _CustomPageInfo = value;
        }
    }

    private int _TotalRow = 0;
    [DefaultValue(0), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public int TotalRow
    {
        get { return _TotalRow; }
        set
        {
            _TotalRow = value;
            //
            if (_PageIndex > TotalPage - 1)
            {
                PageIndex = 0;
            }
            else
            {
                PageIndex = _PageIndex;
            }
        }
    }

    private int _PageIndex = 0;
    [DefaultValue(0), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public int PageIndex
    {
        get { return _PageIndex; }
        set
        {
            _PageIndex = value;
            //
            btnFirst.Enabled = (_PageIndex > 0);
            btnPrev.Enabled = (_PageIndex > 0);
            btnLast.Enabled = (_PageIndex < TotalPage - 1);
            btnNext.Enabled = (_PageIndex < TotalPage - 1);
            //
            lblPageInfo.Text = string.Format(!String.IsNullOrEmpty(CustomPageInfo) ? CustomPageInfo : "Page {0}/{1} ({2} rows)", _PageIndex + 1, TotalPage, _TotalRow.ToString("#,##0"));
        }
    }

    [Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public int TotalPage
    {
        get
        {
            if (_PageSize > 0)
            {
                return (int)decimal.Ceiling(_TotalRow / _PageSize) + (_TotalRow % _PageSize == 0 ? 0 : 1);
            }
            else
            {
                return 0;
            }
        }
    }

    private void btnFirst_Click(System.Object sender, System.EventArgs e)
    {
        PageIndex = 0;
        if (PageIndex_Changed != null)
        {
            PageIndex_Changed(_PageIndex);
        }
    }

    private void btnPrev_Click(System.Object sender, System.EventArgs e)
    {
        PageIndex -= 1;
        if (PageIndex_Changed != null)
        {
            PageIndex_Changed(_PageIndex);
        }
    }

    private void btnNext_Click(System.Object sender, System.EventArgs e)
    {
        PageIndex += 1;
        if (PageIndex_Changed != null)
        {
            PageIndex_Changed(_PageIndex);
        }
    }

    private void btnLast_Click(System.Object sender, System.EventArgs e)
    {
        PageIndex = TotalPage - 1;
        if (PageIndex_Changed != null)
        {
            PageIndex_Changed(_PageIndex);
        }
    }

    private void cboPageSize_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
        try
        {
            if (e.KeyChar.ToString() == Globals.vbCr)
            {
                cboPageSize_Validated(cboPageSize, null);
            }
        }
        catch (Exception ex)
        {
            FormGlobals.Message_Error(ex);
        }
    }

    private void cboPageSize_Validated(object sender, System.EventArgs e)
    {
        try
        {
            int iPageSize = 0;
            if (cboPageSize.Text.ToUpper() == "ALL" || Int32.TryParse(cboPageSize.Text,out iPageSize) )
            {
                _PageSize = cboPageSize.Text.ToUpper() == "ALL" ? 999999 : iPageSize;
                if (PageIndex_Changed != null)
                {
                    PageIndex_Changed(_PageIndex);
                }
            }
            if (iPageSize <= 0 && cboPageSize.Text.ToUpper() != "ALL")
            {
                FormGlobals.Message_Warning("Invalid page size, please input positive integer value");
                cboPageSize.Text = _PageSize.ToString();
            }
        }
        catch (Exception ex)
        {
            FormGlobals.Message_Error(ex);
        }
    }

    private void PagerControl_Load(object sender, System.EventArgs e)
    {
        //if (this.Width < 380)
        //    this.Width = 380;
        if (!AllowChangePageSize)
            lblPageSize.Visible = cboPageSize.Visible = false;
        //
        //this.Enabled = this.TotalPage > 0;
    }
}
